package com.mymoney.persistence.managers;

import java.sql.Timestamp;
import java.util.List;

import com.mymoney.beans.DeviceBean;
import com.mymoney.exceptions.MyMoneyException;
import com.mymoney.persistence.PersistenceContext;

/**
 * 
 * @author yavor.gologanov
 * 
 */
public class DeviceManager extends AbstractBeanManager<DeviceBean> {


	public DeviceManager(PersistenceContext persistenceContext) {
		super(persistenceContext);
	}


	@Override
	public void add(DeviceBean device) throws MyMoneyException {
		
		DeviceBean existingDevice = null;
		if(device != null)
		{
			if(!device.getPrimaryAccount().isEmpty())
			{
				try {
					existingDevice = getByName(device.getPrimaryAccount());
				} catch (MyMoneyException e) {
					e.printStackTrace();
				} 
			}
		}
		if(existingDevice != null)
		{
			update(device,existingDevice);
			device.setDeviceUid(existingDevice.getDeviceUid());
		}
		else
		{
			device.setDeviceUid(PersistenceContext.createUUID());
			device.setDateRegistred(new Timestamp(System.currentTimeMillis()));
			storeEntity(device);
		}
		
	}

	@Override
	public List<DeviceBean> list() throws MyMoneyException {

		String queryString = "select d from DeviceBean d";
		List<DeviceBean> currencyList = findAllEntities(queryString, -1,(Object[]) null);
		return currencyList;
	}

	@Override
	public DeviceBean getByCode(long code) throws MyMoneyException {
		
		throw new MyMoneyException("Method is not implemented!");
	}

	@Override
	public DeviceBean getById(Integer id) throws MyMoneyException {
		
		return getBy("select p from DeviceBean p where p.id = :id", id, "id");
	}

	@Override
	public DeviceBean getByName(String primaryAccount) throws MyMoneyException {
		
		return getBy("select p from DeviceBean p where p.primaryAccount = :primaryAccount", primaryAccount, "primaryAccount");
	}

	@Override
	public void update(DeviceBean device, DeviceBean existingDevice)
			throws MyMoneyException {
		
		if (device.getDeviceName() != null) {
			existingDevice.setDeviceName(device.getDeviceName());
		}

		if (device.getOsVersion() != null) {
			existingDevice.setOsVersion(device.getOsVersion());
		}

		if (device.getCountry() != null) {
			existingDevice.setCountry(device.getCountry());
		}

		if (device.getTimeZone() != null) {
			existingDevice.setTimeZone(device.getTimeZone());
		}

		existingDevice.setLastModifiedDate(new Timestamp(System.currentTimeMillis()));

		device.setId(existingDevice.getId());
		
	}

	@Override
	public void update(DeviceBean entity, long code) throws MyMoneyException {
		throw new MyMoneyException("Method is not implemented!");
		
	}

	@Override
	public void update(DeviceBean entity, Integer id) throws MyMoneyException {
		throw new MyMoneyException("Method is not implemented!");
		
	}

	@Override
	public void update(DeviceBean device, String uid) throws MyMoneyException {

		DeviceBean existingDevice = getByName(uid);

		if (existingDevice == null) {
			throw new MyMoneyException("The device with uid " + uid
					+ " does not exist!");
		}

		update(device, existingDevice);
	}
}